package com.fengba.nanzhi.tabbar04.wz;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONException;
import org.json.JSONObject;

public class WebViewActivity extends AppCompatActivity {

    private WebView webview;
    private String invite_code;
    private ImageButton mBtntjmfh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_web_view);
        mBtntjmfh = findViewById(R.id.tjmfh);
        mBtntjmfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Object user_info = Preferences.getParam(WebViewActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());
            //账号
            invite_code = myJson.get("invite_code").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        webview = findViewById(R.id.wv);
        webview.loadUrl("http://nan-zhi.com/app/friend?parent_invite_code="+invite_code);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);

        //设置是否出现缩放工具
        webview.getSettings().setBuiltInZoomControls(true);
        //设置可以支持缩放
        webview.getSettings().setSupportZoom(true);
        //扩大比例的缩放
        webview.getSettings().setUseWideViewPort(true);

        //设置编码为utf-8
        webview.getSettings().setDefaultTextEncodingName("utf-8");

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
        //设置 缓存模式
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webview.getSettings().setDomStorageEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return false;
    }


}

